#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include "pg_common.h"

#define PASSWD_FILE   "passwd.file"

int main(int argc, char **argv) {

   FILE *fp;
        struct passwd password;
   char login[100], *pass="x", name[100], dir[50], shell[50];
        char *outfile;
        CONNECTION_DB conn;
   PGresult *result=NULL;
        int num_res=0,i;

        if(argc == 2)
           outfile = strdup(argv[1]);
        else
           outfile = strdup(PASSWD_FILE);

   conn = init_db(DB_ARQ_CONF);

   result = PQexec(conn->pgconn, "SELECT * FROM user_sys");
        num_res = PQntuples(result);

   fp = fopen(outfile, "w");
   for(i=0;i<num_res;i++) {

      strcpy(login, PQgetvalue(result,i,2));
      strcpy(name, PQgetvalue(result,i,3));
      strcpy(dir, PQgetvalue(result,i,4));
      strcpy(shell, PQgetvalue(result,i,5));

      password.pw_name=login;
      password.pw_passwd=pass;
      password.pw_uid = atoi(PQgetvalue(result,i,0));
      password.pw_gid = atoi(PQgetvalue(result,i,1));
      password.pw_gecos=name;
      password.pw_dir=dir;
      password.pw_shell=shell;

      putpwent( &password, fp);

   }

   fclose(fp);
   return 0;

}
